#! /usr/bin/python
import strips_engine
def  parseProblem(listed):
	iterator = iter(listed)

	if (iterator.next() != "define"):
		raise SyntaxError("STRIPS problem file does not contain define")

	problem_definition = iterator.next()
	#Check for errors in the problem file
	if (problem_definition[0] == "problem" and len(problem_definition) == 2):
		yield problem_definition[1]
	else:
		raise SyntaxError("STRIPS problem file is not structured properly")

	domain_description = iterator.next()
	#Check for issues in the problem's domain description
	if (domain_description[0] == ":domain" and len(domain_description) == 2):
		yield domain_description[1]
	else:
		raise SyntaxError("STRIPS problem file does not contain a domain description")   	
	requirements = iterator.next()
	if requirements[0] != ":requirements" or len(requirements) != 2: 
		objects = requirements
	else:
		objects = iterator.next()

	if objects[0] == ":objects":
		clean_objects = strips_engine.typed_list_objects(objects[1:] )
		yield clean_objects

		init = iterator.next()
	else:
		yield []
		init = objects

	if (init[0] != ":init"): raise SyntaxError("STRIPS problem contains no init state")
	clean_init = [strips_engine.atom(x) for x in init[1:]]
	yield clean_init

	goal = iterator.next()

	if (goal[0] == ":goal" and len(goal) == 2):
		clean_goal = strips_engine.conjunction(goal[1],strips_engine.atom)
		yield clean_goal
	else:
		raise SyntaxError("STRIPS problem contains no goal state")

if __name__ == "__main__":
	listed = parse_input_file(open("problem1.pddl"))
	clean_listed = parseProblem(listed)
	print [x for x in clean_listed]

